<template>
  <div class="login_background">
    <div class="login_box">
      <!-- 头像区域 -->
      <img src="@/assets/image/timg.jpeg" />
      <!-- 登录表单区域 -->
      <el-form
        class="login_from"
        :model="loginFrom"
        :rules="rules"
        ref="ruleForm2"
        status-icon
        label-width="0px"
      >
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input
            v-model="loginFrom.username"
            prefix-icon="iconfont icon-user"
          ></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
            v-model="loginFrom.password"
            type="password"
            prefix-icon="iconfont icon-3702mima"
          ></el-input>
        </el-form-item>
        <!-- 按钮区域 -->
        <el-form-item class="but">
          <el-button type="primary" @click="submitForm('ruleForm2')">
            登录
          </el-button>
          <el-button type="info" @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 这是登录表单的数据绑定对象
      loginFrom: {
        username: "admin",
        password: "www.xiaomadagege.cn"
      },
      // 这是表单的验证规则对象
      rules: {
        // 验证用户名是否合法
        username: [
          { required: true, message: "请输入登录名称", trigger: "blur" },
          { min: 3, max: 10, message: "长度在 3 到 10 个字符", trigger: "blur" }
        ],
        // 验证密码是否合法
        password: [
          { required: true, message: "请输入用户密码", trigger: "blur" },
          { min: 3, max: 20, message: "长度在 3 到 20 个字符", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    // 点击登录按钮，
    submitForm(formName) {
      this.$refs[formName].validate(async valid => {
        if (valid) {
          const result = await this.$http.post("login", this.loginFrom);
          if (result.meta.status == 200) {
            this.$message.success("登录成功！");
            window.sessionStorage.setItem("token", result.data.token); // 将token存储到本地浏览器中
            this.$router.push({ path: "/home" });
          } else {
            this.$message.error("登录失败！");
          }
        } else {
          return false;
        }
      });
    },
    // 点击重置按钮，重置表单
    resetForm() {
      this.$refs.ruleForm2.resetFields();
      console.log(this.token);
    }
  }
};
</script>

<style lang="stylus" scoped>
.login_background {
  height: 100%;
  background: #2b4b6b;

  .login_box {
    width: 450px;
    height: 300px;
    background: #fff;
    border-radius: 4px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);

    img {
      width: 40%;
      border-radius: 50%;
      position: absolute;
      left: 50%;
      transform: translate(-50%, -30%);
    }
  }
}

.but {
  display: flex;
  justify-content: flex-end;
}

.login_from {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}
</style>
